﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text.RegularExpressions;
using System.Text;

public partial class search_result : System.Web.UI.Page
{
    public string rawKeyword = "";

    public const string USER_PAGE = "my/index.aspx?no=";

    protected void Page_Load(object sender, EventArgs e)
    {
       
        searchit(getKeyword());
    }

    private String getKeyword()
    {
        object keyword = Request[Citiport.UI.Const.SEARCH_KEYWORD_PARAMETER_NAME];
        if (keyword == null)
            return null;
        else
            return DataUtil.ObjetToString(keyword);
    }

    void searchit(string text)
    {
        ctpDataContext db = new ctpDataContext();
        rawKeyword = text.Trim();
        text = text.Trim().ToLower();

        var city_search = from p in db.ct_profiles
                          join q in db.ct_language_texts.Where(a => a.from_table_no == (int)DatabaseUtil.fromTables.ct_profile_title)
                          on p.no equals q.post_no
                          where p.title == text || q.text.Contains(text)
                          select new { result = DecroKeywordHTML(rawKeyword,p.title), 
                              result_url = "city.aspx?no=" + p.no,
                              main_pic_fn = p.main_pic_fn.Trim()
                          };

        var country_search = from p in db.ct_profiles
                             join q in db.ct_language_texts.Where(a => a.from_table_no == (int)DatabaseUtil.fromTables.ct_profile_country)
                             on p.no equals q.post_no
                             where p.country == text || q.text.Contains(text)
                             select new { result = DecroKeywordHTML(rawKeyword, p.country), result_url = p.no };

        var spot_search = from p in db.ct_s_profiles
                          join q in db.ct_language_texts.Where(a => a.from_table_no == (int)DatabaseUtil.fromTables.s_profile_title)
                          on p.no equals q.post_no
                          where (p.title == text || q.text.Contains(text)) && p.valid == true
                          select new { result = DecroKeywordHTML(rawKeyword, p.title), 
                              result_url = "city_s.aspx?no=" + p.no,
                              main_pic_fn = p.main_pic_fn
                          };
        var user_search = from p in db.users
                          where (p.nickname.Contains(text) || p.email.Contains(text))
                          select new
                          {
                              result = UserDisplayString(rawKeyword, p.email, p.nickname),
                              result_url = USER_PAGE + p.no,
                          main_pic_fn = p.profile_picture};

        int city_count = city_search.Count();
        int country_count = country_search.Count();
        int spot_count = spot_search.Count();
        int user_count = user_search.Count();
        if (city_count< 1 && country_count < 1 && spot_count < 1 && user_count <1)
            this.search_no_result_div.Visible = true;
        else
        {
            if (city_count > 0)
            {
                this.span_city.Visible = true;
                this.DataList1.DataSource = city_search.Distinct();
            }
            if (country_count > 0)
            {
                this.span_country.Visible = true; 
                this.DataList2.DataSource = country_search.Distinct();
            }
            if (spot_count > 0)
            {
                this.span_hotspot.Visible = true;
                this.DataList3.DataSource = spot_search.Distinct();
            }
            if (user_count > 0)
            {
                this.span_user.Visible = true;
                this.DataList4.DataSource = user_search.Distinct();
            }
           

            this.DataBind();
        }

        //if (city_search.Count() != 0)
        //{
         //   Response.Redirect("city.aspx?no=" + city_search.First().no.ToString());
        //}

        //if (spot_search.Count() != 0)
        //{
        //    Response.Redirect("city_s.aspx?no=" + spot_search.First().no.ToString());

        //}


        //else
        //{
        //    Response.Redirect("no_this_city.aspx?city=" + this.TextBox1.Text);
        //}

        //if (country_search.Count() != 0)
        //{
         //   this.GridView1.DataSource = country_search;
        //    this.DataBind();
        //
        //}
    }

    public const String KEYWORD_CSS_CLASS = "cityport_search_keyowrd_b";

    private String DecroKeywordHTML(String keyword, String result)
    {
        if (DataUtil.IsEmptyString(result))
            return null;
      
       
        MatchCollection mc = Regex.Matches(result, keyword, RegexOptions.IgnoreCase);
        foreach (Match m in mc)
        {
            String oldString = m.Value;
            String newString = "<b class=\""+KEYWORD_CSS_CLASS+"\" >"+oldString+"</b>";
            result = result.Replace(result.Substring(m.Index,m.Length), newString);
        }
        return result;//Regex.Replace(result.Trim(), keyword, newString, RegexOptions.IgnoreCase); 
    }

    private String UserDisplayString(String keyword, String email, String nickname)
    {
        StringBuilder sb = new StringBuilder(DecroKeywordHTML(keyword,nickname));
        sb.Append(": " +DecroKeywordHTML(keyword, email));
        return sb.ToString();
    }
  
}
